package com.itheima79.googleplay.ui.fragment;

import com.itheima79.googleplay.R;
import com.itheima79.googleplay.adapter.BasicAdapter;
import com.itheima79.googleplay.adapter.HomeAdapter;
import com.itheima79.googleplay.adapter.HomeHeaderAdapter;
import com.itheima79.googleplay.bean.AppInfo;
import com.itheima79.googleplay.bean.Home;
import com.itheima79.googleplay.http.Api;
import com.itheima79.googleplay.http.HttpUtil;
import com.itheima79.googleplay.ui.activity.AppDetailActivity;
import com.itheima79.googleplay.util.CommonUtil;
import com.itheima79.googleplay.util.JsonUtil;
import com.itheima79.googleplay.util.LogUtil;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;

public class HomeFragment extends BaseListFragment<AppInfo> {
	private ViewPager viewPager;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			//让ViewPager选中下一页
			viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
			//重新发消息
			handler.sendEmptyMessageDelayed(0, 2500);
			
			LogUtil.e(this, "handleresg");
		};
	};
	
//	@Override
//	protected View getSuccessView() {
//		refreshListView = (PullToRefreshListView) View.inflate(getActivity(), R.layout.ptr_listview, null);
//		//1.设置刷新模式
//		refreshListView.setMode(Mode.BOTH);//设置既可以上拉也可以下拉
//		//2.设置刷新监听器
//		refreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
//			/**
//			 * 下拉刷新和上拉加载更多都会执行该方法，
//			 * @param refreshView
//			 */
//			@Override
//			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
//				//直接调用
//				loadingPage.loadDataAndRefreshPage();
//			}
//		});
//		//3.获取PullToRefreshListView内部包裹的ListView
//		listView = refreshListView.getRefreshableView();
//		listView.setDividerHeight(0);//去掉分割线
//		listView.setSelector(android.R.color.transparent);//将listveiw的selector设置为透明
//		//4.添加头部
//		addHeader();
//		//5.填充数据
//		homeAdapter = new HomeAdapter(list);
//		listView.setAdapter(homeAdapter);
//		
//		return refreshListView;
//	}
	
	@Override
	protected BasicAdapter<AppInfo> getAdapter() {
		return new HomeAdapter(list);
	}
 	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getActivity(),AppDetailActivity.class);
		intent.putExtra("packageName",list.get(position-2).getPackageName());
		startActivity(intent);
	}
	
	/**
	 * 添加头部
	 */
	protected void addHeader(){
		View headerView = View.inflate(getActivity(), R.layout.layout_home_header, null);
		viewPager = (ViewPager) headerView.findViewById(R.id.viewPager);
		//根据图片的宽高比，去动态设定viewPager的高度，让它的宽高比和图片能保持一致
		//1.获取屏幕的宽度
		int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
		//2.根据图片的宽高比获取对应的高度,(宽高比是2.65)
		float height = width/2.65f;
		//3.将高度设置给viewPager
		LayoutParams params = viewPager.getLayoutParams();
		params.height = (int) height;
		viewPager.setLayoutParams(params);
		
		listView.addHeaderView(headerView);
	}
	
 	@Override
	protected Object requestData() {
		checkPullFromStart();
 		
 		String result = HttpUtil.get(Api.Home+list.size());
		
		//2.解析json数据
		final Home home = JsonUtil.parseJsonToBean(result, Home.class);
		
		if(home!=null){
			list.addAll(home.getList());//更新数据
			CommonUtil.runOnUIThread(new Runnable() {
				@Override
				public void run() {
					//由于只在第0页才有picture的数据，所以需要增加判断
					if(home.getPicture()!=null && home.getPicture().size()>0){
						//显示轮播图的数据
						viewPager.setAdapter(new HomeHeaderAdapter(home.getPicture()));
						
						//设置默认选中比较大的一页
						viewPager.setCurrentItem(home.getPicture().size()*100000);
					}
					
					adapter.notifyDataSetChanged();//更新UI
					//完成刷新
					refreshListView.onRefreshComplete();
				}
			});
		}
		
		return home;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		//重新发消息
		handler.sendEmptyMessageDelayed(0, 2500);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		//移除消息
		handler.removeMessages(0);
	}

 	
}
